import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'CardFree.dart';
import 'CardRecommend.dart';
import 'CardShare.dart';
import 'CardSpecial.dart';
import 'CustomAppBar.dart';

class ContentPager extends StatefulWidget {
  final ValueChanged<int> onPageChanged;

  final ContentPagerController contentPagerController;

  //构造方法   Key:默认的构造方法  可选参数
  const ContentPager({Key key, this.onPageChanged, this.contentPagerController})
      /**初始化列表**/
      : super(key: key);

  @override
  _ContentPagerState createState() => _ContentPagerState();
}

class _ContentPagerState extends State<ContentPager> {
  PageController _pageController = PageController(
      //视图比例
      viewportFraction: 0.8);


  @override
  void initState() {
    if (widget.contentPagerController != null) {
      widget.contentPagerController._pageController = _pageController;
    }
    //设置状态栏
    _statusBar();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    //实现上下布局  阿萨擦
    return Column(
      children: <Widget>[
        //设置appBar
        CustomAppBar(),

        //Expanded 充满 高度撑开
        Expanded(
            //PageView 可滑动的组件
            child: PageView(
          controller: _pageController,
          onPageChanged: widget.onPageChanged,
          children: <Widget>[
            _wrapItem(CardRecommend()),
            _wrapItem(CardShare()),
            _wrapItem(CardFree()),
            _wrapItem(CardSpecial()),
          ],
        ))
      ],
    );
  }

  //返回一个带边距的容器
  Widget _wrapItem(Widget widget) {
    return Padding(
      padding: EdgeInsets.all(10),
      child: widget,
    );
  }

  _statusBar() {
    SystemUiOverlayStyle uiOverlayStyle = SystemUiOverlayStyle(
      systemNavigationBarColor: Color(0xFF000000),
      systemNavigationBarDividerColor: null,
      statusBarColor: Colors.red,
      systemNavigationBarIconBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness: Brightness.light,
    );
    SystemChrome.setSystemUIOverlayStyle(uiOverlayStyle);
  }
}

//内容区域控制器
class ContentPagerController {
  PageController _pageController;

  //改变pageView的指针页面
  void jumpToPage(int page) {
    //dart  安全调用 ?.   防止空指针
    _pageController?.jumpToPage(page);
  }
}
